var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
const {startBrowser, getCurrentTime} = require('./utils/utils')
var indexRouter = require('./routes/index');
var deviceRouter = require('./routes/devices');
var openaiRouter = require('./routes/openai');
var sseRouter = require('./routes/sse');
const {errorData} = require("./utils/response");
var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');

app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({extended: false}));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.all('/*', function (req, res, next) {
  // console.log(`request start==========>>>${getCurrentTime()} url=>${req.url} \n ip=>${req.ip} \n headers=>${JSON.stringify(req.headers)}\n params=>${JSON.stringify(req.params)} \n body=${JSON.stringify(req.body)}<<<==========`)
  //在这里手动配置
  let mHost = ["116.204.91.36:3000", "192.168.43.222:3000", "localhost:3000", "api.next.bspapp.com", "static-mp-67dcfd0c-c143-49dd-81a6-3d6aabd6ef7b.next.bspapp.com"]
  if (mHost.find(item => req.headers.host.toLowerCase().indexOf(item) != -1)) next()
  else errorData(res, "304")
  // next()
});

app.use('/', indexRouter);
app.use('/device', deviceRouter);
app.use('/openai', openaiRouter);
app.use('/sse', sseRouter);
//静态文件托管
app.use(express.static('static'));

// startBrowser()
module.exports = app;
